/**
 * Copyright (C) 2013-2014 EaseMob Technologies. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.pocketsweet.chatui.receiver;

import java.util.List;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.FindCallback;
import com.avos.avoscloud.GetCallback;
import com.easemob.chat.EMChatManager;
import com.pocketsweet.MLApplication;
import com.pocketsweet.MLCache;
import com.pocketsweet.chat.ui.activity.RandomCallActivity;
import com.pocketsweet.chat.ui.activity.RandomCallMatchingActivity;
import com.pocketsweet.chat.ui.activity.VoiceCallActivity;
import com.pocketsweet.chatui.MLHXSDKHelper;
import com.pocketsweet.chatui.domain.User;
import com.pocketsweet.model.MLLovers;
import com.pocketsweet.model.MLUser;
import com.pocketsweet.service.UserService;
import com.pocketsweet.ui.DialogLoveCall;
import com.pocketsweet.ui.Main;

public class CallReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(final Context context, Intent intent) {
		if (!MLHXSDKHelper.getInstance().isLogined())
			return;

		// call type
		String type = intent.getStringExtra("type");
		if ("video".equals(type)) { // 视频通话这里转为情侣随机通话
			if (RandomCallMatchingActivity.getInstance() == null) {// 当收到随机电话，自己不在页面中就删掉记录
				EMChatManager.getInstance().endCall();
			} else {
				// 拨打方username
				String from = intent.getStringExtra("from");
				AVQuery<MLUser> query = new AVQuery<MLUser>("_User");
				query.getInBackground(from, new GetCallback<MLUser>() {

					@Override
					public void done(MLUser user, AVException e) {
						if (e == null) {
							context.startActivity(new Intent(context,
									RandomCallActivity.class)
									.putExtra("username", user.getObjectId())
									.putExtra("nickname", user.getNickname())
									.putExtra("isComingCall", true)
									.putExtra("avatar", user.getAvatarUrl())
									.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
						}
					}
				});

			}

		} else { // 音频通话
			if (RandomCallMatchingActivity.getInstance() != null) {// 当在随机电话的页面时候，拒接其他电话；
				EMChatManager.getInstance().endCall();
				return;
			}
			if (Main.customazitonLoverId != null) {// 定制电话打进了了
				String from = intent.getStringExtra("from");
				AVQuery<MLUser> query = new AVQuery<MLUser>("_User");
				query.getInBackground(from, new GetCallback<MLUser>() {

					@Override
					public void done(MLUser user, AVException e) {
						if (e == null) {
							context.startActivity(new Intent(context,
									VoiceCallActivity.class)
									.putExtra("username", user.getObjectId())
									.putExtra("nickname", user.getNickname())
									.putExtra("isComingCall", true)
									.putExtra("avatar", user.getAvatarUrl())
									.putExtra("isCustomazition", true)
									.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
						}
					}
				});

			} else {
				// 拨打方username
				String from = intent.getStringExtra("from");
				String nickname = MLCache.getNickById(from);
				String avatar = MLCache.getAvatarById(from);
				context.startActivity(new Intent(context,
						VoiceCallActivity.class).putExtra("username", from)
						.putExtra("nickname", nickname)
						.putExtra("isComingCall", true)
						.putExtra("avatar", avatar)
						.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
			}

		}
		// EMLog.d("CallReceiver", "app received a incoming call");
	}

}
